var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [],
    address: "",
    truename: "",
    nickName: "",
    gender: "",
    sfzid: "",
    tel: "",
    email: "",
    city:"重庆"
  },

  /**
   * 生命周期函数--监听页面加载
   */
 
  onLoad: function(options) {
    this.getUserInfo()
    var _this = this
    wx.getStorage({
      key: 'cityName',
      success: function (res) {

        _this.setData({
          city: res.data
        })
      }
    })
  },
  onShow: function () {
    this.getUserInfo()
    var _this = this
    wx.getStorage({
      key: 'cityName',
      success: function (res) {
        _this.setData({
          city: res.data
        })
      }
    })
 
    
  },
  getUserInfo: function() {
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: 'https://miniapp.odeman.com/summercamp/useredit.php',
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: {
        userid: wx.getStorageSync('userid'),
        option: 'info'
      },
      method:'get',
      success(res) {
        wx.hideLoading()
        var data = res.data
        if (data.status == "success") {
          var list = data.lists
          var sex = []
          if (list.gender == "1") {
            // 代表是男
            sex = [{
                name: '男',
                value: '男',
                checked: 'true'
              },
              {
                name: '女',
                value: '女'
              }
            ]
          } else {
            sex = [{
                name: '男',
                value: '男',
              },
              {
                name: '女',
                value: '女',
                checked: 'true'
              }
            ]
          }
          that.setData({
            headimgurl: list.headimgurl,
            username: list.username,
            truename: list.truename,
            nickname: list.nickname,
            gender: list.gender,
            sfzid: list.sfzid,
            tel: list.tel,
            email: list.email,
            mext: list.mext,
            items: sex
          })
        }
      }
    })
  },
  user: function(e) {
    this.setData({
      truename: e.detail.value
    })
  },
  idCard: function(e) {
    this.setData({
      sfzid: e.detail.value
    })
  },
  address: function(e) {
    this.setData({
      address: e.detail.value
    })
  },
  nickname: function(e) {
    this.setData({
      nickname: e.detail.value
    })
  },
  radioChange: function(e) {
    var sex = ""
    if (e.detail.value == "女") {
      sex = "2"
    } else {
      sex = "1"
    }
    this.setData({
      gender: sex
    })
  },
  saveUserInfo: function(e) {
    var that = this
    var idCardRegex = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
    if (!idCardRegex.test(this.data.sfzid)) {
      wx.showModal({
        title: '温馨提示',
        content: '请输入正确的身份证号码'
      })
    } else if (this.data.address == "") {
      wx.showModal({
        title: '温馨提示',
        content: '请填写地址'
      })
    } else {
      wx.showLoading({
        title:'加载中'
      })
      wx.request({
        url: 'https://miniapp.odeman.com/summercamp/useredit.php',
        data: {
          option:'edit',
          truename: that.data.truename,
          nickname: that.data.nickname,
          gender: that.data.gender,
          sfzid: that.data.sfzid,
          address: that.data.address,
          userid: wx.getStorageSync('userid')
        },
        method:'post',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success(res) {
          wx.hideLoading()
          if (res.data.status == "success") {
            wx.showToast({
              title: '认证成功',
              icon: 'success',
              duration: 3000
            })
            setTimeout(function(){
              wx.navigateBack({
                delta: 1
            })
            },3000)
          
          } else {
            wx.showToast({
              title: '认正失败',
              icon: 'success',
              duration: 3000
            })
          }
        }
      })
    }

  }
})